package edu.ptu.shortlink.project.toolkit;


import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import jakarta.servlet.http.HttpServletRequest;
import ua_parser.Client;
import ua_parser.Parser;


import java.util.Date;
import java.util.Optional;

import static edu.ptu.shortlink.project.common.constant.ShortLinkConstant.SHORT_LINK_CACHE_VALID_DATE;

/**
 * @BelongsProject: shortlink
 * @BelongsPackage: edu.ptu.shortlink.project.toolkit
 * @Author: HYJ
 * @CreateTime: 2025-05-01  00:32
 * @Description: 短链接工具类
 * @Version: 1.0
 */
public class LinkUtil {
    /**
     * 获取短链接缓存有效期
     *
     * @param validDate 有效期
     * @return 有效期
     */
    public static long getShortLintCacheValidDate(Date validDate) {

        return Optional.ofNullable(validDate)
                .map(date -> DateUtil.between(new Date(), date, DateUnit.MS))
                .orElse(SHORT_LINK_CACHE_VALID_DATE);
    }

    /**
     * 获取真实请求ip
     *
     * @param req 请求
     * @return ip
     */
    public static String getActualIp(HttpServletRequest req) {
        String ipAddress = req.getHeader("X-Forwarded-For");

        if (ipAddress == null || ipAddress.isEmpty() || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = req.getHeader("Proxy-Client-IP");
        }
        if (ipAddress == null || ipAddress.isEmpty() || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = req.getHeader("WL-Proxy-Client-IP");
        }
        if (ipAddress == null || ipAddress.isEmpty() || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = req.getHeader("HTTP_CLIENT_IP");
        }
        if (ipAddress == null || ipAddress.isEmpty() || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = req.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ipAddress == null || ipAddress.isEmpty() || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = req.getRemoteAddr();
        }
        return ipAddress;
    }

    /**
     * 获取操作系统
     *
     * @param req 请求
     * @return 操作系统
     */
    public static String getOs(HttpServletRequest req) {
        String userAgent = req.getHeader("User-Agent");
        Parser parser = new Parser();

        ua_parser.Client client = parser.parse(userAgent);
        return client.os.family;
    }

    /**
     * 获取浏览器
     *
     * @param req 请求
     * @return 浏览器
     */
    public static String getBrowser(HttpServletRequest req) {
        String userAgent = req.getHeader("User-Agent");
        Parser parser = new Parser();

        Client client = parser.parse(userAgent);
        return client.userAgent.family;
    }

    /**
     * 获取设备
     *
     * @param req 请求
     * @return 设备
     */
    public static String getDevice(HttpServletRequest req) {
        String userAgent = req.getHeader("User-Agent");

        if (userAgent.toLowerCase().contains("mobile")) {
            return "Mobile";
        }
        return "PC";
    }

    /**
     * 获取网络
     *
     * @param req 请求
     * @return 网络
     */
    public static String getNetwork(HttpServletRequest req) {
        String actualIp = getActualIp(req);
        return actualIp.startsWith("192.168.") || actualIp.startsWith("10.") ? "WIFI" : "Mobile";
    }
}
